package com.taptech.doufu.ui.view.choice;

import android.content.Context;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.taptech.doufu.R;
import com.taptech.doufu.bean.EventBean;
import com.taptech.doufu.bean.choice.ChoiceTopicBean;
import com.taptech.doufu.bean.choice.NovelModel;
import com.taptech.doufu.ui.view.PullToRefreshListView;
import com.taptech.doufu.ui.view.choice.adapter.ChoiceTopicOtherAdapter;
import com.taptech.doufu.ui.view.topicview.MeasuredLinearLayoutManager;
import com.taptech.doufu.weex.http.TFHttpError;
import com.taptech.doufu.weex.http.TFHttpResponse;
import com.taptech.doufu.weex.http.TFHttpUtil;

import java.util.HashMap;
import java.util.List;

public class ChoiceTopicRecommendView extends LinearLayout {
    private RecyclerView mRecyclerView;
    private String moreUrl;
    private TextView tvTitle;
    private TextView tvSubTitle;
    private EventBean eventData;
    private ChoiceTopicOtherAdapter adapter;
    private PullToRefreshListView listView;

    public ChoiceTopicRecommendView(Context context) {
        super(context);
    }

    public ChoiceTopicRecommendView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ChoiceTopicRecommendView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public ChoiceTopicRecommendView(Context context, final ChoiceTopicBean bean, PullToRefreshListView listView) {
        this(context);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.topic_choice_recommend_view, null);
        mRecyclerView = view.findViewById(R.id.mRecyclerView);
        tvTitle = view.findViewById(R.id.tvTitle);
        tvSubTitle = view.findViewById(R.id.tvSubTitle);
        moreUrl = bean.getMoreUrl();
        tvTitle.setText(bean.getTitle());
        eventData = bean.getEventData();
        if (!TextUtils.isEmpty(bean.getSubTitle())) {
            tvSubTitle.setVisibility(View.VISIBLE);
            tvSubTitle.setText(bean.getSubTitle());
        }else{
            tvSubTitle.setVisibility(View.GONE);
        }
        setData(bean.getNovelList());
        this.listView = listView;
        if (listView != null) {
            listView.setOnLoadMoreListener(() -> getData(moreUrl));
        }

        addView(view);
    }


    public void setData(List<NovelModel> list) {
        MeasuredLinearLayoutManager manager = new MeasuredLinearLayoutManager(getContext());
        adapter = new ChoiceTopicOtherAdapter(getContext(), list,eventData);
        manager.setScrollEnabled(false);
        mRecyclerView.setLayoutManager(manager);
        mRecyclerView.setAdapter(adapter);
    }



    private void getData(String url) {
        url = url.replace("https","http");
        if (TextUtils.isEmpty(url)) {
            return;
        }
        if (listView != null) {
            listView.setLoadmoreable(false);
        }
        TFHttpUtil.requestGet(url, new HashMap(), new TFHttpResponse() {
            @Override
            public void onResponse(JSONObject json, TFHttpError error) {
                if (listView != null) {
                    listView.onRefreshComplete();
                }
                if (json != null && json.containsKey("data") &&
                        json.getJSONObject("data")!=null && json.getJSONObject("data").containsKey("list")) {
                    JSONObject jsonObject = json.getJSONObject("data");
                    String array = jsonObject.getString("list");
                    if (jsonObject.containsKey("moreUrl")) {
                        moreUrl = jsonObject.getString("moreUrl");
                    }
                    List<NovelModel> list = JSON.parseArray(array, NovelModel.class);
                    if (list.size() > 0) {
                        if (adapter != null) {
                            adapter.addDatas(list);
                        }else{
                            setData(list);
                        }
                        if (listView != null) {
                            listView.loadMoreComplete();
                            listView.setLoadmoreable(true);
                        }
                    }else{
                        if (listView != null) {
                            if (listView.getLoadCompleteImg() != null) {
                                listView.getLoadCompleteImg().setImageResource(R.drawable.draw_recommand_bottom_img);
                            }
                            listView.setFull(true);
                            listView.loadMoreComplete();
                            listView.setLoadmoreable(false);
                        }
                    }
                }else{
                    if (listView != null) {
                        listView.loadMoreComplete();
                        listView.setLoadmoreable(true);
                    }
                }


            }
        });

    }
}
